package com.web;


import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Comment;
import com.entity.Trend;
import com.entity.UserProject;
import com.service.TrendService;
import com.util.DateHelp;
import com.util.PageHelp;
import com.util.StringHelp;
import com.util.UUIDHelp;

public class TrendServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TrendService ts=new TrendService();
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session=request.getSession();
		UserProject up=(UserProject) session.getAttribute("up");
		
		String method=request.getParameter("m");
		String hm=request.getMethod();
		new StringHelp();
		if(StringHelp.isEmpty(method)){
			list(request,response,up);
		}else if("say".equals(method)){
			if("POST".endsWith(hm)){
				say(request,response,up);
			}
		}else if("trend".equals(method)){
			addTrend(request,response,up);
		}else if("list2".equals(method)){
			list2(request,response);
		}
	}
	private void list2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String trendid=request.getParameter("id");
		List<Comment> CommentList=ts.findComment(trendid);
		
		request.setAttribute("CommentList", CommentList);
		request.getRequestDispatcher("/WEB-INF/pms/project.jsp").forward(request, response);
		
	}
	private void addTrend(HttpServletRequest request,
			HttpServletResponse response, UserProject up) {
		// TODO Auto-generated method stub
		String trend=request.getParameter("content");
		Trend t=new Trend();
		t.setId(UUIDHelp.executeUUID());
		t.setContent(trend);
		t.setCreateTime(DateHelp.getTime());
		t.setUserid(up.getUserid());
		
		ts.saveTrend(t);
		
	}
	private void say(HttpServletRequest request, HttpServletResponse response, UserProject up) throws IOException {
		String message=request.getParameter("message");
		String id=request.getParameter("id");
		
		Comment c=new Comment();
		c.setCreateTime(DateHelp.getTime());
		c.setId(UUIDHelp.executeUUID());
		c.setMessage(message);
		c.setTrendid(id);
		c.setUserid(up.getUserid());
		ts.saveFromTrend(c);
		
		response.sendRedirect("trend.jspx");
	}
	private void list(HttpServletRequest request, HttpServletResponse response, UserProject up) throws ServletException, IOException {
		String p=request.getParameter("p");
		int pageNum=0;
		if(StringHelp.isEmpty(p)){
			pageNum=1;
		}else {
			pageNum=Integer.parseInt("p");
		}
		PageHelp<Trend> page=ts.findTrendByPage(pageNum);
		request.setAttribute("page", page);
		request.setAttribute("up", up);
		for(Trend list:page.getResult()){
			request.setAttribute("tim",list.getCreateTime());
			request.setAttribute("tin",DateHelp.showTime(list.getCreateTime()));
		}
	
		request.getRequestDispatcher("/WEB-INF/pms/project.jsp").forward(request, response);
	}
}
